package com.doge.service.admin.promotion;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.pojo.goods.GoodsExample;
import com.doge.bean.pojo.topic.Topic;
import com.doge.bean.pojo.topic.TopicExample;
import com.doge.bean.vo.topic.TopicReadVO;
import com.doge.exception.Delete502Exception;
import com.doge.exception.Param602Exception;
import com.doge.mapper.GoodsMapper;
import com.doge.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * promotion是负责推广模块
 * TopicService是专题管理
 * Created By ntian
 */
@Service
public class TopicServiceImpl implements TopicService{
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public BaseData listTopic(BaseParam baseParam, String title, String subtitle) {
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if(!StringUtils.isEmpty(title)){
            criteria.andTitleLike("%"+title+"%");
        }
        if (!StringUtils.isEmpty(subtitle)){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        example.setOrderByClause(baseParam.getSort()+" "+baseParam.getOrder());
        List<Topic> topicList = topicMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(topicList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        BaseData data = BaseData.list(topicList, total);
        data.setLimit(baseParam.getLimit());
        data.setPage(baseParam.getPage());
        data.setPages(pages);
        return data;

    }

    @Override
    public Topic createTopic(Topic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        int rows = topicMapper.insertSelective(topic);
        return topic;
    }

    @Override
    public Topic updateTopic(Topic topic) {
        topic.setUpdateTime(new Date());
        int rows = topicMapper.updateByPrimaryKeySelective(topic);
        return topic;
    }

    /**
     * 主要是在编辑页面上回显数据需要，最下面有商品list
     * @param id 专题的id
     * @return TopicReadVO，一个专门为了回显专题编辑页面数据的VO
     */
    @Override
    public TopicReadVO readTopic(Integer id) {
        TopicReadVO topicReadVO = new TopicReadVO();
        //尝试使用 resultMap 联合查询，失败，哭哭...
        //TopicReadVO topicReadVO = topicMapper.selectTopicReadVOById(id);
        Topic topic = topicMapper.selectByPrimaryKey(id);
        // goods是goods里面的Id
        Integer[] goodsIds = topic.getGoods();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
        List<Integer> ids = Arrays.asList(goodsIds);
        topicReadVO.setGoodsList(new ArrayList<>());
        if (ids.size() != 0){
            goodsCriteria.andIdIn(ids);
            List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
            topicReadVO.setGoodsList(goodsList);
        }
        topicReadVO.setTopic(topic);
        return topicReadVO;
    }

    @Override
    public int deleteTopic(Topic topic) throws Exception {
        topic.setDeleted(true);
        int i = topicMapper.updateByPrimaryKeySelective(topic);
        if (i!=1){
            throw new Delete502Exception("删除失败！");
        }
        return i;
    }

    /**
     * 对专题广告进行批量删除
     * @param ids 专题的id
     * @return 被删除的rows
     * @throws Exception
     */
    @Override
    public int batchDelete(List<Integer> ids) throws Exception {
        int rows = topicMapper.updateTopic(ids);
        if (rows!=ids.size()){
            throw new Delete502Exception("删除失败！");
        }
        return rows;
    }
}